package org.yi.fc.controller.admin.login;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.yi.fc.common.Constants;
import org.yi.fc.controller.base.BaseController;
import org.yi.fc.entity.FSysUser;
import org.yi.fc.ext.render.FCCaptchaRender;

public class LoginController extends BaseController {
	
	private static final Logger logger = LoggerFactory.getLogger(LoginController.class);

	public void login() {
		
		/*if(METHOD_GTE.equalsIgnoreCase(getRequest().getMethod())){
			logger.trace("forward login page...");
			forwardAction("/admin/index");
		} else if(METHOD_POST.equalsIgnoreCase(getRequest().getMethod())){
			logger.trace("do login...");
			String capthcaCode = getPara(Constants.VERIFY_CODE);
			String codeInSession = getSessionAttr(Constants.VERIFY_CODE);
			
			if(codeInSession == null || !codeInSession.equalsIgnoreCase(capthcaCode)) {
				addError("验证码错误！");
				render("login.html");
			} else {
				String username = getPara("username");
				String passwd = getPara("passwd");
				FSysUser user = new FSysUser().uniqueResult(username, passwd);
				
				if(user == null) {
					addError("用户名或密码错误！");
					render("login.html");
				} else {
					setSessionAttr("f_sys_user", user);
					redirect("/admin");
				}
			}
		} else {
			addMessage("方法错误， 请求只接受get和post方法");
			forwardAction( "/error");
		}*/
	}
	/**
	 * 进入首页
	 */
	public void index() {
		if( getSessionAttr("f_sys_user") != null ){
			render("index.html");			
		} else {
			render("login.html");
		}
	}
	/**
	 * 管理后台禁止注册新用户， 只能通过超级管理员在后台添加新用户
	 */
	public void regist(){
		/*if(METHOD_GTE.equalsIgnoreCase(getRequest().getMethod())) {
			//进入注册页面
			render("regist.html");
		} else if(METHOD_POST.equalsIgnoreCase(getRequest().getMethod())) {
			FSysUser user = new FSysUser();
			String username = getPara("username");
			if(!isUserExist(username)) {
				//用户不存在， 则入库
				user.set("username", username)
				   .set("passwd", DigestUtils.md5Hex(getPara("passwd")+"{"+getPara("username")+"}"))
		  		   .save();
				setSessionAttr("f_sys_user", user);
				redirect("/admin/index");
			} else {
				saveError("%s 已存在！", username);
				render("regist.html");
			}
		} else {
			saveError("方法错误， 请求只接受get和post方法");
			forwardAction("/error");
		}*/
	}
	
	
	
	/**
	 * ajax判断用户是否可注册
	 */
	public void exists() {
		Map<String,Object> obj = new HashMap<String,Object>();
		obj.put("exist", FSysUser.dao.userExist(getPara("key")));
		renderJson(obj);
	}
	
	/**
	 *  用户退出登录
	 */
	public void logout(){
		destroyResources();
		getSession().invalidate();
		redirect("/admin/index");
	}
	
	/**
	 * 销毁用户占用的系统资源
	 */
	private void destroyResources(){
		logger.trace("destory resources...");
	}
	
	/**
	 * 生成随机验证码
	 */
	public void genCaptchaImg(){
		String codeKey = UUID.randomUUID().toString();
		FCCaptchaRender captchRender = new FCCaptchaRender(codeKey);
		render(captchRender);
	}
	
	/**
	 * 对验证码进行校验
	 */
	public void validateCaptchaCode(){
		String capthcaCode = getPara(Constants.VERIFY_CODE);
		String codeInSession = getSessionAttr(Constants.VERIFY_CODE);
		renderJson("validate",codeInSession ==null || codeInSession.equalsIgnoreCase(capthcaCode));
	}
}
